package net.thxjj.service.impl;


import net.model.device.DeviceBean;
import net.thxjj.service.IDeviceService;
import net.thxjj.service.dao.DeviceDao;
import net.util.ParameterMap;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.List;

@Service
public class DeviceService implements IDeviceService {
    private Logger log = Logger.getLogger(this.getClass());

    @Autowired
    private DeviceDao deviceDao;

    @Override
    public DeviceBean getDevice(ParameterMap parameterMap) {
        DeviceBean deviceBean = deviceDao.getDeviceInfo(parameterMap);
        return deviceBean;
    }


    @Override
    public void updateDeviceOffline(ParameterMap parameterMap) {
        deviceDao.updateDeviceOffline(parameterMap);
    }

    @Override
    public void insertDevNewInfo(ParameterMap parameterMap) {
        try {
            deviceDao.insertDeviceBaseInfo(parameterMap);
        }catch (Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
            log.error("insertDevNewInfo error", e);
        }

    }

    @Override
    public void insertDevSendLog(ParameterMap parameterMap) {
         deviceDao.insertDevSendLog(parameterMap);
    }

    @Override
    public void updateDevBaseInfo(ParameterMap parameterMap) {
        deviceDao.updateDevBaseInfo(parameterMap);
    }

    @Override
    public void updateDevOnlineStatus(ParameterMap parameterMap) {
        deviceDao.updateDevOnlineStatus(parameterMap);
    }

    @Override
    public List<Long> getRelationUserIds(String device_id) {

        return deviceDao.getRelationUserIds(device_id);
    }

    @Override
    public List<DeviceBean> getUserBindDeviceList(long user_id) {

        List<String> devTids = deviceDao.getUserBindDeviceList(user_id);

        List<DeviceBean> list1 = new ArrayList<>();
        for (int i=0;i<devTids.size();i++){
            String pam = devTids.get(i);
            ParameterMap map = new ParameterMap();
            map.put("device_id",pam);
            DeviceBean baseinfo = deviceDao.getDeviceInfo(map);
            list1.add(baseinfo);
        }

        return list1;
    }

    @Override
    public List<String> getUserBindDevTidList(long user_id) {
        return deviceDao.getUserBindDeviceList(user_id);
    }
}
